package cn.yeyiu.sexyiu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.yeyiu.sexyiu.R;
import cn.yeyiu.sexyiu.listener.OnItemClickListener;
import cn.yeyiu.util.MediaUtil;
import cn.yeyiu.util.bean.VideoInfo;

public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.ViewHolder> {
    private final Context context;
    public int cur_page = 0;
    private final LayoutInflater mInflater;
    public int max_page;
    private OnItemClickListener onItemClickListener;
    public int page_size = 9;
    public final MutableLiveData<List<VideoInfo>> videoInfoList = new MutableLiveData<>();

    public void setPageSize(int i) {
        this.page_size = i;
    }

    public String getPage() {
        return String.valueOf(this.cur_page + 1);
    }

    public String getMaxPage() {
        return String.valueOf(this.max_page);
    }

    public void nextPage() {
        this.cur_page = (this.cur_page + 1) % this.max_page;
        notifyDataSetChanged();
    }

    public void prePage() {
        int i = this.max_page;
        this.cur_page = ((this.cur_page - 1) + i) % i;
        notifyDataSetChanged();
    }

    public void update() {
        if (this.videoInfoList.getValue() != null) {
            this.max_page = (int) Math.ceil((double) (((float) this.videoInfoList.getValue().size()) / ((float) this.page_size)));
            notifyDataSetChanged();
        }
    }

    public VideoListAdapter(Context context2) {
        this.context = context2;
        this.mInflater = LayoutInflater.from(context2);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = this.mInflater.inflate(R.layout.item_data, viewGroup, false);
        inflate.setOnClickListener(v->{
            this.onItemClickListener.onItemClick(v);
        });
        return new ViewHolder(inflate);
    }

    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        if (this.videoInfoList.getValue() != null) {
            VideoInfo videoInfo = this.videoInfoList.getValue().get((this.page_size * this.cur_page) + i);
            MediaUtil.fixLocalVideoInfo(videoInfo);

            viewHolder.title.setText(videoInfo.title);
            Glide.with(this.context)
                    .load(videoInfo.picSrc)
                    .placeholder(R.drawable.jpg_home_header)
                    .into(viewHolder.image);
        }
    }

    @Override // androidx.recyclerview.widget.RecyclerView.Adapter
    public int getItemCount() {
        if (this.videoInfoList.getValue() == null) {
            return 0;
        }
        int size = this.videoInfoList.getValue().size();
        int i = this.page_size;
        return Math.min(size - (this.cur_page * i), i);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener2) {
        this.onItemClickListener = onItemClickListener2;
    }

    /* access modifiers changed from: package-private */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView image;
        private final TextView title;

        public ViewHolder(View view) {
            super(view);
            this.image = (ImageView) view.findViewById(R.id.data_pic_img);
            this.title = (TextView) view.findViewById(R.id.data_title_tv);
        }
    }
}
